# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=perl
pkgver=5.40.0
pkgrel=3
pkgdesc="Larry Wall's Practical Extraction and Report Language"
url="https://www.perl.org/"
arch="all"
license="Artistic-1.0-Perl OR GPL-1.0-or-later"
depends_dev="perl-utils=$pkgver-r$pkgrel"
makedepends="bzip2-dev zlib-dev"
subpackages="$pkgname-doc $pkgname-dev $pkgname-utils::noarch miniperl"
source="https://www.cpan.org/src/5.0/perl-$pkgver.tar.xz
	digest-sha-cflags.patch
	dont-write-packlist.patch
	json-pp-options.patch
	musl-skip-dst-test.patch
	musl-stack-size.patch
	skip-test-due-to-busybox-ps.patch
	strftime-fmt.patch
	"
# creates empty usr/local/{lib,share} for local sitedirs
options="!fhs"

# in perl core, removed from main/perl-scalar-list-utils
provides="
	perl-scalar-list-utils=$pkgver-r$pkgrel
	"

# secfixes:
#   5.38.1-r0:
#     - CVE-2023-47038
#   5.34.0-r1:
#     - CVE-2021-36770
#   5.30.3-r0:
#     - CVE-2020-10543
#     - CVE-2020-10878
#     - CVE-2020-12723
#   5.26.3-r0:
#     - CVE-2018-18311
#     - CVE-2018-18312
#     - CVE-2018-18313
#     - CVE-2018-18314
#   5.26.2-r1:
#     - CVE-2018-12015
#   5.26.2-r0:
#     - CVE-2018-6797
#     - CVE-2018-6798
#     - CVE-2018-6913
#   5.26.1-r0:
#     - CVE-2017-12837
#     - CVE-2017-12883

_privlib=/usr/share/perl5/core_perl
_archlib=/usr/lib/perl5/core_perl

prepare() {
	# most sources are readonly but we patch some
	chmod +w "$builddir"/*.c
	default_prepare

	# Ensure that we never accidentally bundle zlib or bzip2
	rm -rf cpan/Compress-Raw-Zlib/zlib-src
	rm -rf cpan/Compress-Raw-Bzip2/bzip2-src
	sed -i '/\(bzip2\|zlib\)-src/d' MANIFEST
}

build() {
	export BUILD_ZLIB=0
	export BUILD_BZIP2=0
	export BZIP2_LIB=/usr/lib
	export BZIP2_INCLUDE=/usr/include

	# language runtime
	export CFLAGS="$CFLAGS -O2 -flto=auto"

	./Configure -des \
		-Dcccdlflags='-fPIC' \
		-Dccdlflags='-rdynamic' \
		-Dprefix=/usr \
		-Dprivlib=$_privlib \
		-Darchlib=$_archlib \
		-Dvendorprefix=/usr \
		-Dvendorlib=/usr/share/perl5/vendor_perl \
		-Dvendorarch=/usr/lib/perl5/vendor_perl \
		-Dsiteprefix=/usr/local \
		-Dsitelib=/usr/local/share/perl5/site_perl \
		-Dsitearch=/usr/local/lib/perl5/site_perl \
		-Dlocincpth=' ' \
		-Doptimize="$CFLAGS" \
		-Duselargefiles \
		-Dusethreads \
		-Duseshrplib \
		-Dd_semctl_semun \
		-Dman1dir=/usr/share/man/man1 \
		-Dman3dir=/usr/share/man/man3 \
		-Dinstallman1dir=/usr/share/man/man1 \
		-Dinstallman3dir=/usr/share/man/man3 \
		-Dman1ext='1' \
		-Dman3ext='3pm' \
		-Dcf_by='Alpine' \
		-Dcf_email='buildozer@alpinelinux.org' \
		-Ud_csh \
		-Ud_fpos64_t \
		-Ud_off64_t \
		-Dusenm \
		-Duse64bitint
	make libperl.so && make
}

check() {
	export CI=true
	export LC_ALL=C

	# Perl tests HARNESS_OPTIONS against the regex
	# /^j(\d*)$/, if $JOBS is unset, it defaults to 9
	export HARNESS_OPTIONS=j"$JOBS"
	export PERL_TEST_HARNESS_ASAP=1

	make test_harness_notty
}

package() {
	make DESTDIR="$pkgdir" install.perl

	if subpackages_has "$pkgname-doc"; then
		make install.man \
			DESTDIR="$pkgdir" \
			INSTALLFLAGS="--man3ext=3pm"
	fi

	# omit global flto afterward
	# perl saves compile-time cflags and applies them to every future build
	sed -i \
		-e "s| -flto=auto||g" \
		"$pkgdir"/usr/lib/perl5/core_perl/Config_heavy.pl

	if [ -n "$(find "$pkgdir"/usr/local -type f)" ]; then
		error "files found under /usr/local"
		return 1
	fi

	install -Dm755 miniperl \
		-t "$pkgdir"/usr/bin/
}

miniperl() {
	amove usr/bin/miniperl
}

doc() {
	local file; find "$pkgdir" -name "*.pod" | while read -r file; do
		amove "${file#"$pkgdir"}"
	done
	default_doc
}

dev() {
	amove \
		$_archlib/Devel \
		$_privlib/Encode \
		usr/bin/h2xs \
		usr/bin/perlivp \
		usr/bin/enc2xs \
		usr/bin/xsubpp

	default_dev
}

utils() {
	pkgdesc="$pkgdesc (misc utilities)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove \
		usr/bin/corelist \
		usr/bin/cpan \
		usr/bin/encguess \
		usr/bin/libnetcfg \
		usr/bin/h2ph \
		usr/bin/instmodsh \
		usr/bin/json_pp \
		usr/bin/perlbug \
		usr/bin/perlthanks \
		usr/bin/piconv \
		usr/bin/pl2pm \
		usr/bin/prove \
		usr/bin/ptar \
		usr/bin/ptardiff \
		usr/bin/ptargrep \
		usr/bin/shasum \
		usr/bin/splain \
		usr/bin/zipdetails
}

sha512sums="
a2fb1a24c6367b4043f4e929b2d74fc3bad1415e53b791ed1f219f1701064ae21b2bd3164ba95fcf24eaf458bd54433024ccae43725c0bb82a1ec6a98dc7052d  perl-5.40.0.tar.xz
59afa4c166e4808d355e19cd70748540ffce9da5c6919c71648be7678de328409f9121ddea33415add73fc01a22e95ed9d9629f31b8ba20b3bbfc04dab926c63  digest-sha-cflags.patch
abae709290306a8fbc96b604b9b38fc09a46ae178f3e77944813f1b46c78e2e3654e5c3b3a6aca96684a2d1061e2871376d508fbe82604864055309156b4d0d8  dont-write-packlist.patch
4e8a655160e54a151e0bc4819fd1cd69e43c1e36844bcefbb9734f36130a2d9e1700b1aa64153a9d20a8deb875e904876dbe7b14dd278266b21a86a33d91eebd  json-pp-options.patch
3eaec691bc38e208ba4f34130ae45e50b6c339fa50093d0b0f6d7f24cb5b7c2a3734ca714a279c4b51173f82e46a8e7a3e70bfc7d7471a18c55a2102140e4186  musl-skip-dst-test.patch
c004d6612ec754e5947255a2e2d15b5581f187c32495aeeec9f4fa286919bd9f40c72b63db61e3f4004b09288af2063a6a14b67e5c289e9a8b23ebd7c216e16f  musl-stack-size.patch
ba9cb1ff4a6e8e4c31bf4ef132c071ac919ffe45842cf5e98c8ca72d78ef4803883b57a0e7b8037da0079561a6ce9e6bd8d127892ac1bcb047638cb3ba3157f6  skip-test-due-to-busybox-ps.patch
df9cbb50326beecc49f5a59af267e02965bee5d56b852edcb0d2ab57689d080fedb2ddad110d05b97215c999a51573d540e8a8a8bbd94a39b5464d9bdedc51de  strftime-fmt.patch
"
